﻿using Google.Protobuf.WellKnownTypes;
using Mobileterminal.Common;
using Mobileterminal.Dtos;
using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mobileterminal.Forms.Zyy
{
    public partial class UpUsage : UIEditForm
    {
        private UsageDTO app; // 使用你的DTO类
        public UpUsage(string UsageId)
        {
            InitializeComponent();
            this.Load += async (sender, e) => await LoadDataAsync(UsageId);
        }

        private void UpUsage_Load(object sender, EventArgs e)
        {

        }
      

        private void uiLabel3_Click(object sender, EventArgs e)
        {

        }

        private void uiRadioButton2_CheckedChanged(object sender, EventArgs e)
        {
        }

        private async void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                // 更新数据模型
                app.Usagenumber = uiTextBox1.Text;
                app.Registrationdepartment = uiComboBox1.Text;
                app.registrar = uiTextBox2.Text;
           
                app.Status = uiRadioButton1.Checked ? 1 : 2;
              
                // 序列化并创建请求内容
                var json = JsonConvert.SerializeObject(app);
                HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");
                
                // 发送PUT请求
                var responseJson = await HttpClientHelper.ClientAsync(
                    "PUT",
                    "https://localhost:7286/api/USage/UpdateUsageUasge",  // 修正URL拼写
                    true,
                    httpContent
                );

                if (string.IsNullOrEmpty(responseJson))
                {
                    MessageBox.Show("服务器返回空响应，请检查服务端状态或请求数据。");
                    return;
                }

                // 反序列化响应
                var result = JsonConvert.DeserializeObject<ShelfsDTO>(responseJson);
                if (result != null)
                {
                    MessageBox.Show("修改成功");
                    this.DialogResult = DialogResult.OK;  // 先设置结果
                    this.Close();

                }
                else
                {
                    MessageBox.Show("反序列化失败，响应内容格式不正确。");
                }
            }
            catch (HttpRequestException ex)
            {
                MessageBox.Show($"网络请求失败：{ex.Message}");
            }
            catch (JsonException ex)
            {
                MessageBox.Show($"解析响应失败：{ex.Message}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}");
            }
        }
        public static List<UsageDTO> appsTypes = new List<UsageDTO>();
      



        private async Task LoadDataAsync(string UsageId)
        {
            var url = $"https://localhost:7224/api/Usage/FanUsage?UsageId={UsageId}";
            var json = HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<UsageDTO>>>(await json);
            if (result != null && result.Data != null && result.Data.Count > 0)
            {
                app = result.Data[0];
                uiTextBox1.Text = app.Usagenumber;
                uiComboBox1.Text = app.Registrationdepartment;
                uiTextBox2.Text = app.registrar;
                uiRadioButton2.Checked = app.Status == 1;
                uiRadioButton1.Checked = app.Status == 2;


            }
        }


        private void uiTextBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
